<?php
/**
 * Created by PhpStorm.
 * User: o0无忧亦无怖(285753421@qq.com)
 * Date: 2015/7/4
 * Time: 16:26
 */

class wechat_menu extends MY_Admin_Controller {


    function __construct(){
        parent::__construct();
        $this->assign('index_channel','marketing');
    }

    function set_menu(){
        $this->load->model('Wechat_keyword_model');
        $conditions=array('is_del'=>0);
        $options['order'] = 'id';
        $options['limit'] = array(20,0);//取最近20个  limit offset
        @$result = $this->Wechat_keyword_model->fetch_array($conditions,'keyword',$options);
        $this->config->load('wechat', TRUE);
        $wechat_config = $this->config->item('wechat');
        $wechat_menu =$wechat_config['wechat_menu'];
        $this->assign('wechat_menu',$wechat_menu);
        $this->assign('keyword',$result);
        $this->display();
    }

    function do_save_menu(){
        $this->config->load('wechat', TRUE);
        $wechat_config = $this->config->item('wechat');
        $wechat_menu =$wechat_config['wechat_menu'];
        $type=$this->input->post('button_type');
        $value = $this->input->post('value');
        $father = $this->input->post('father');
        $name = $this->input->post('name');
        if($this->input->post('action')=='add'){
            if($this->input->post('type')==1){
                $count=count($wechat_menu);
                if($count<3){
                    $wechat_menu[$count]['type'] = $type;
                    $wechat_menu[$count]['name'] = $name;
                    $wechat_menu[$count]['value'] = $value;
                }else{
                    $this->json_response(false,'','1级菜单超过3个');
                }
            }else{
                $count = count($wechat_menu[$father]['son']);
                if($count<5) {
                    $wechat_menu[$father]['son'][$count]['type'] = $type;
                    $wechat_menu[$father]['son'][$count]['name'] = $name;
                    $wechat_menu[$father]['son'][$count]['value'] = $value;
                }
                else{
                    $this->json_response(false,'','2级菜单超过5个');
                }
            }
        }
        if($this->input->post('action')=='del'){
            if($this->input->post('type')==1){
                $id=$this->input->post('id');
                unset($wechat_menu[$id]);
            }else{
                $son_id = $this->input->post('son_id');
                unset($wechat_menu[$father]['son'][$son_id]);
            }
        }
        $wechat_config['wechat_menu'] = $wechat_menu;
        $this->config->set_item('wechat', $wechat_config);
        $this->config->save('wechat');
        $this->json_response(true);
    }

    function do_set_menu(){
        $access_token='';
        $this->load->config('wechat',true);
        $config = $this->config->item('wechat');
        $options=array(
            'token'     =>  $config['token'],
            'appid'     =>  $config['appid'],
            'appsecret' =>  $config['appsecret'],
            'encodingaeskey'    =>  $config['encodingaeskey']
        );
        $wechat_menu = $config['wechat_menu'];
        $data['button'] = array();
        foreach($wechat_menu as $key=>$val){
            if(isset($val['son'])&&is_array($val['son'])){
                $data['button'][$key]['name']=$val['name'];
                foreach($val['son'] as $keys=>$vals){
                    if($vals['type']=='url'){
                        $data['button'][$key]['sub_button'][$keys]['type']='view';
                        $data['button'][$key]['sub_button'][$keys]['name']=$vals['name'];
                        $data['button'][$key]['sub_button'][$keys]['url']=$vals['value'];
                    }
                    if($vals['type']=='key'){
                        $data['button'][$key]['sub_button'][$keys]['type']='click';
                        $data['button'][$key]['sub_button'][$keys]['name']=$vals['name'];
                        $data['button'][$key]['sub_button'][$keys]['key']=$vals['value'];
                    }
                    $data['button'][$key]['sub_button'][$keys]['sub_button']=array();
                }
            }else{
                $data['button'][$key]['name'] =$val['name'];
                if($val['type']=='url') {
                    $data['button'][$key]['type'] = 'view';

                    $data['button'][$key]['url']  = $val['value'];
                }
                if($vals['type']=='text'){
                    $data['button'][$key]['type'] = 'click';
                    $data['button'][$key]['key']  = $val['value'];
                }

            }
        }
        $this->load->library('Wechat_action',$options);
        //从缓存文件里读取token 并验证
        //不存在就重新获取
        if(!$this->wechat_action->getCache('access')){
            $access_token = $this->wechat_action->checkAuth($config['appid'],$config['appsecret']);
            if($access_token) {
                $this->wechat_action->setCache($access_token, time() + 100, 7200, 'access');
            }else{
                $access_token=$config['access']['access_token'];
                $this->wechat_action->checkAuth($config['appid'],$config['appsecret'],$access_token);
            }
        }
    //    echo $config['access']['access_token'];
        if($this->wechat_action->createMenu($data)){
            $this->json_response(true);
        }
        $this->json_response(false);
    }
    function a(){
        $this->load->config('wechat',true);
        $config = $this->config->item('wechat');
        echo '<pre>';
        //var_dump($config);
        $options=array(
            'token'     =>  $config['token'],
            'appid'     =>  $config['appid'],
            'appsecret' =>  $config['appsecret'],
            'encodingaeskey'    =>  $config['encodingaeskey']
        );
        $this->load->library('Wechat_action',$options);
        //从缓存文件里读取token 并验证
      /*  $access_token = $this->wechat_action->checkAuth($config['appid'],$config['appsecret']);
       /* $this->wechat_action->setCache($access_token, time() + 100, 7200, 'access');
        var_dump($access_token);*/
      $access_token= $this->wechat_action->getCache('access');
        var_dump($access_token);

    }
}